package responses

import (
	"github.com/edgexfoundry/edgex-go/internal/app/dtos"
	common2 "github.com/edgexfoundry/edgex-go/internal/app/dtos/common"
	"github.com/edgexfoundry/edgex-go/internal/app/models"
	edgexDtos "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
	"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
)

type DeviceResourcesResponse[T common2.PageEntity] struct {
	common.BaseResponse `json:",inline"`
	common2.PageInfo[T]
}

// NewDeviceResourcesResponse creates deviceResource response DTO with required fields
func NewDeviceResourcesResponse(requestId string, message string, statusCode int, resources []models.DeviceResource, page, pageSize int, totalCount uint32) DeviceResourcesResponse[dtos.DeviceResource] {

	pageInfo := common2.NewPageInfo(page, pageSize, totalCount, ToDeviceResourceDTOs(resources))
	return DeviceResourcesResponse[dtos.DeviceResource]{
		BaseResponse: common.NewBaseResponse(requestId, message, statusCode),
		PageInfo:     pageInfo,
	}
}

func ToDeviceResourceDTOs(drs []models.DeviceResource) []dtos.DeviceResource {
	deviceResources := make([]dtos.DeviceResource, len(drs))
	for index, dr := range drs {
		deviceResources[index] = ToDeviceResourceDTO(dr)
	}
	return deviceResources
}

func ToDeviceResourceDTO(d models.DeviceResource) dtos.DeviceResource {
	return dtos.DeviceResource{
		DBTimestamp: edgexDtos.DBTimestamp{
			Created:  d.Created,
			Modified: d.Modified,
		},
		DeviceResource: edgexDtos.FromDeviceResourceModelToDTO(d.DeviceResource),
		Code:           d.Code,
		Id:             d.Id,
	}
}
